package com.hotelManagerSystem.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.hotelManagerSystem.model.*;
import com.hotelManagerSystem.model.CleaningRecord;

import com.hotelManagerSystem.service.*;

import com.hotelManagerSystem.service.ICleaningRecordService;
import com.hotelManagerSystem.util.MD5Util;
import com.hotelManagerSystem.util.PageUtil;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.crypto.spec.DESedeKeySpec;
import javax.jws.WebParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  职工前端控制器
 * </p>
 *
 * @author longyajr
 * @since 2021-12-24
 */
@Controller
@RequestMapping("/staff")
public class StaffController {
    @Autowired
    private IStaffService iStaffService;
    @Autowired
    private IDepartmentService iDepartmentService;
    @Autowired
    private IDepartmentStaffService iDepartmentStaffService;
    @Autowired
    private ICleaningRecordService iCleaningRecordService;


    ////////////////////////////////系统管理员进行职工得注册,

    /*
    思路:后台管理系统不能直接注册用户,只能通过后台超级管理员进行添加,删除,修改
     */
    //一:查询所有

//    职工列表
    @RequestMapping("/list")
    public String list(Model model,Integer pageNum,Integer pageSize){
        PageUtil.runStartPage(pageNum,pageSize);
        List<StaffDepartment> staffDepartmentList = iStaffService.queryStaffDepartmentList();
        PageInfo pageInfo = PageUtil.getPageInfo(staffDepartmentList);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/staff/staff-list";
    }

//    职工添加页面跳转
    @RequestMapping("/addUI")
    public String addUI(Model model){
//        System.out.println("------------addUI---------------");
        List<Department> departmentList = iDepartmentService.list();
        model.addAttribute("departmentList",departmentList);
        return "admin/staff/staff-add";
    }

    @RequestMapping("/queryStaffByUsername")
    public String queryStaffByUsername(@RequestParam("username") String username,Integer pageNum,Integer pageSize,Model model){
        PageUtil.runStartPage(pageNum,pageSize);
        List<StaffDepartment> staffDepartment = iStaffService.queryStaffByUsername(username);
        System.out.println(staffDepartment);


        PageInfo pageInfo = PageUtil.getPageInfo(staffDepartment);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/staff/staff-list";
    }
    //校验用户名
    @RequestMapping("/checkUsername")
    @ResponseBody
    public Map<String,Object> checkUsername(String username){
        Map<String,Object> map = new HashMap<>();
        Staff staff = iStaffService.queryByUsername(username);
        if(staff!=null){
            //已经存在职工
            map.put("code",204);
            map.put("message","该职工已经存在,请勿重复添加");
        }else{
            map.put("code",200);
        }
        return map;
    }
//    职工添加
    @RequestMapping("add")
    @ResponseBody
    public Map<String,Object> add(Staff staff,DepartmentStaff departmentStaff){
        Map<String,Object> map = new HashMap<>();
        String salt = MD5Util.createRandomStr1();
        staff.setSalt(salt);
        String password = MD5Util.md5(salt, staff.getPassword());
        staff.setPassword(password);
        //默认设置为存在
        staff.setStaffState(1);
        boolean b = iStaffService.save(staff);
        Staff staff1 = iStaffService.queryByUsername(staff.getUsername());
        Integer staffId = staff1.getStaffId();
        departmentStaff.setStaffId(staffId);
        if(b){
            boolean b1 = iDepartmentStaffService.save(departmentStaff);
            if(b1){
                map.put("code",200);
                map.put("message","添加成功");
            }else{
                map.put("code",204);
                map.put("message","添加失败");
            }
        }else{
            map.put("code",205);
            map.put("message","添加失败");
        }
        return map;
    }

//    职工删除
    @RequestMapping("delete")
    @ResponseBody
    public Map<String,Object> delete(Model model,String staffIdStr){
        Map<String,Object> map = new HashMap<>();
        int staffId = Integer.parseInt(staffIdStr);
        boolean b = iStaffService.removeById(staffId);
        if(b){
            map.put("code",200);
            map.put("message","删除记录成功");
        }else{
            map.put("code",204);
            map.put("message","删除记录失败");
        }
        return map;
    }

//    职工编辑页面跳转
    @RequestMapping("updateUI")
    public String updateUI(Model model,String staffId){
        System.out.println(staffId+"--------------");
        int staffIdTemp = Integer.parseInt(staffId);
        StaffDepartment staffDepartment =iStaffService.selectOneStaff(staffIdTemp);
        model.addAttribute("staffDepartment",staffDepartment);
        List<Department> departmentList = iDepartmentService.list();
        model.addAttribute("departmentList",departmentList);
        return "admin/staff/staff-edit";
    }

//    职工编辑
    @RequestMapping("update")
    @ResponseBody
    public Map<String,Object> update(Staff staff,DepartmentStaff departmentStaff){
        Map<String,Object> map = new HashMap<>();
        System.out.println(staff);
        System.out.println(departmentStaff);
        //将传输的密码转换为MD5加密
        String password = MD5Util.md5(staff.getUsername(), staff.getPassword());
        staff.setPassword(password);
        boolean b = iStaffService.updateStaff(staff);
        boolean b1 = iDepartmentStaffService.updateDepartmentIdByStaffId(departmentStaff);
        if(b||b1){
                map.put("code",200);
                map.put("message","修改成功");
            }else{
                map.put("code",204);
                map.put("message","修改失败");
            }
        return map;
    }




    /**
     * @author treasy
     * @description 根据部门查询未正在打扫的员工的信息
     */
    @RequestMapping("/queryIdleStaff")
    public String queryStaffByCleanOrderState(Model model,int cleaningRecordId){
        //根据id查询订单号
        CleaningRecord cleaningRecord = iCleaningRecordService.getById(cleaningRecordId);
        //根据部门查询未正在打扫的员工的信息
        List<Staff> staffList = iStaffService.queryStaffByCleanOrderState();
        model.addAttribute("staffList",staffList);
        model.addAttribute("cleaningRecord",cleaningRecord);
        return "/admin/cleanPage/clean-edit";
    }





    //职工黑白名单
    //一:查询所有,跳转到black-white.staff.html显示数据
    @RequestMapping("/black-white-staff")
    public String black_white(Model model,Integer pageNum,Integer pageSize){
        PageUtil.runStartPage(pageNum,pageSize);
        List<Staff> staffList = iStaffService.queryStaffList();
        PageInfo pageInfo = PageUtil.getPageInfo(staffList);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/hwlist/black-white-staff";
    }
    //二:黑名单功能---根据职工的id把该职工添加进黑名单
    @RequestMapping("/addStaffById")
    public String addStaffById(String staffIdStr){
        int staffId = Integer.parseInt(staffIdStr);
        int i = iStaffService.addStaffById(staffId);
        return "redirect:/staff/black-white-staff";
    }
    //三:黑名单功能:根据职工的id把该该职工从黑名单中移除(即黑名单的删除)
    @RequestMapping("/deleteStaffById")
    public String deleteStaffById(String staffIdStr){
        int staffId = Integer.parseInt(staffIdStr);
        int i = iStaffService.deleteStaffById(staffId);
        return "redirect:/staff/black-white-staff";
    }
    //四:查询所有黑名单职工
    @RequestMapping("/blackStaffList")
    public String blackStaffList(Model model,Integer pageNum,Integer pageSize){
        PageUtil.runStartPage(pageNum,pageSize);
        List<Staff> staffList = iStaffService.selectBlackStaffList();
        PageInfo pageInfo = PageUtil.getPageInfo(staffList);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/hwlist/black-white-staff";
    }

    //五:查询所有白名单职工
    @RequestMapping("/whiteStaffList")
    public String whiteUserList(Model model,Integer pageNum,Integer pageSize){
        PageUtil.runStartPage(pageNum,pageSize);
        List<Staff> staffList = iStaffService.selectWhiteStaffList();
        PageInfo pageInfo = PageUtil.getPageInfo(staffList);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/hwlist/black-white-staff";
    }
   //六:用户名具有唯一性,使用用户名进行查询职工,从来根据查询到的数据进行黑名单的增加和删除
    @RequestMapping("/selectStaffByUsername")
    public String selectStaffByUsername(@RequestParam("username")String username, Model  model,Integer pageNum,Integer pageSize){
        PageUtil.runStartPage(pageNum,pageSize);
        List<Staff> staffList =iStaffService.selectStaffByUsername(username);
        PageInfo pageInfo = PageUtil.getPageInfo(staffList);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/hwlist/black-white-staff";
    }






}

